package com.demo.general;

import com.demo.common.model.Khsx2;
import com.jfinal.plugin.activerecord.Page;

/**
 * 本 demo 仅表达最为粗浅的 jfinal 用法，更为有价值的实用的企业级用法
 * 详见 JFinal 俱乐部: http://jfinal.com/club
 * 
 * XhfydService
 * 所有 sql 与业务逻辑写在 Service 中，不要放在 Model 中，更不
 * 要放在 Controller 中，养成好习惯，有利于大型项目的开发与维护
 */
public class Khsx2Service {
	
	/**
	 * 所有的 dao 对象也放在 Service 中，并且声明为 private，避免 sql 满天飞
	 * sql 只放在业务层，或者放在外部 sql 模板，用模板引擎管理：
	 * 			http://www.jfinal.com/doc/5-13
	 */
	private Khsx2 dao = new Khsx2().dao();
	
	public Page<Khsx2> paginate(int pageNumber, int pageSize) {
		return dao.paginate(pageNumber, pageSize, "select * ", "from Khsx2 order by RQ desc");
	}
	public Page<Khsx2> paginate(int pageNumber, int pageSize,String startDate, String endDate, String djbh, String gysdm, String gysmc, String fkfs) {
		StringBuilder sql = new StringBuilder();
		sql.append(" from KHSX2");
//		.append(" join (select DJBH,fs.FSMC from XHFYDMX mx join SFKFS fs on fs.FSDM = mx.BYZD3 group by DJBH,fs.FSMC) mx on mx.DJBH = fy.DJBH")
//		.append(" join KEHU kh on kh.KHDM = fy.DM2 WHERE 1 = 1");
//		if(StringUtil.isNotBlank(startDate)) {
//			sql.append(" AND RQ >= '"+startDate+"'");
//		}
//		if(StringUtil.isNotBlank(endDate)) {
//			sql.append(" AND RQ <= '"+endDate+"'");
//		}
		sql.append(" ORDER BY RQ DESC");
		return dao.paginate(pageNumber, pageSize, "select *", sql.toString());
	}
	
	public Khsx2 findById(String id) {
		return dao.findById(id);
	}
	
	public Khsx2 findByKHDM(String KHDM){
		StringBuilder sql = new StringBuilder();
		sql.append("select sx.* from KHSX2 sx")
			.append(" join KEHU kh on sx.SXDM = kh.KHSX2")
			.append(" where kh.KHDM = '"+KHDM+"'");
		Khsx2 sx = dao.findFirst(sql.toString());
		return sx;
	}
	
	public void deleteById(String id) {
		dao.deleteById(id);
	}
}
